﻿// FrameworkElementExtensions.cs
// Copyright (c) Nikhil Kothari, 2008. All Rights Reserved.
// http://www.nikhilk.net
//
// Silverlight.FX is an application framework for building RIAs with Silverlight.
// This project is licensed under the BSD license. See the accompanying License.txt
// file for more information.
// For updated project information please visit http://projects.nikhilk.net/SilverlightFX.
//

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace System.Windows {

    internal static class FrameworkElementExtensions {

#if SILVERLIGHT
        public static FrameworkElement FindChild(this FrameworkElement element, string name)
        {
            int i = VisualTreeHelper.GetChildrenCount(element);

            for (int g = 0; g < i; g++)
            {
                FrameworkElement child = VisualTreeHelper.GetChild(element, g) as FrameworkElement;
                if (child == null)
                    continue;
                FrameworkElement result = child.Name == name ? child : child.FindChild(name);
                if (result != null)
                    return result;
            }
            return null;
        }
#endif
        public static object FindNameRecursive(this FrameworkElement element, string name) {
            FrameworkElement rootVisual = element.GetRootVisual();
            if (rootVisual == null) {
                return null;
            }

            return rootVisual.FindName(name);
        }

        public static object FindRecursive(this FrameworkElement element, Type type) {
            while (element != null) {
                element = element.GetParentVisual();

                if ((element != null) && type.IsAssignableFrom(element.GetType())) {
                    return element;
                }
            }

            return null;
        }

        public static object FindResource(this FrameworkElement element, string key) {
            while (element != null) {
                object value = element.Resources[key];
                if (value != null) {
                    return value;
                }

                element = element.GetParentVisual();
            }

            return null;
        }

        public static FrameworkElement GetParentVisual(this FrameworkElement element) {
            return VisualTreeHelper.GetParent(element) as FrameworkElement;
        }

        public static FrameworkElement GetRootVisual(this FrameworkElement element) {
            FrameworkElement parent = null;
            while (element != null) {
                parent = element;
                if (parent is UserControl) {
                    // HACK: A UserControl parented to another UserControl has a non-null
                    //       parent; however we want to consider the UserControl as the
                    //       root visual for its contents...
                    break;
                }

                element = element.Parent as FrameworkElement;
            }

            return parent;
        }
        public enum RelativeVerticalMousePosition
        {
            Middle,
            Top,
            Bottom
        }

        public static RelativeVerticalMousePosition GetRelativeVerticalMousePosition(FrameworkElement elm, Point pt)
        {
            if (pt.Y > 0.0 && pt.Y < 25)
            {
                return RelativeVerticalMousePosition.Top;
            }
            else if (pt.Y > elm.ActualHeight - 25 && pt.Y < elm.ActualHeight)
            {
                return RelativeVerticalMousePosition.Top;
            }
            return RelativeVerticalMousePosition.Middle;
        }




    }
}
